package com.jingbian.cppccbackend.controller.user;


import com.github.pagehelper.PageInfo;
import com.jingbian.cppccbackend.entity.ProposalSigners;
import com.jingbian.cppccbackend.entity.Result;
import com.jingbian.cppccbackend.entity.dto.ProposalSignersListDTO;
import com.jingbian.cppccbackend.service.user.IProposalSignersService;
import com.jingbian.cppccbackend.utils.ResultUtil;
import com.jingbian.cppccbackend.vo.ProposalSignersListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 提案联名人表 前端控制器
 * </p>
 *
 * @author pum
 * @since 2025-02-12
 */
@RestController
@RequestMapping("/users/signers")
public class ProposalSignersController {
  @Autowired
  private IProposalSignersService proposalSignersService;

  /**
   * 查询我的联名提案
   * @param proposalSignersListDTO
   * return Result<PageInfo<ProposalSignersListVO>>
   */
  @PostMapping("/list")
  public Result<PageInfo<ProposalSignersListVO>> getList(
      @RequestBody ProposalSignersListDTO proposalSignersListDTO
  ) {
    return ResultUtil.success(proposalSignersService.getList(proposalSignersListDTO));
  }

  /**
   * 联名提案处理
   * @param status
   * @param proposalId
   * @return Result<String>
   */
  @PutMapping("/change")
  public Result<ProposalSigners> changeProposalSigners(@RequestParam Integer proposalId,@RequestParam String status) {
    ProposalSigners proposalSigners = proposalSignersService.changeStatus(proposalId,status);
    return ResultUtil.success(proposalSigners);
  }

  /**
   * 取消联名人未处理的提案
   * @param proposalId 提案id
   * @return Result<String>
   */
  @DeleteMapping("/deleteConsignProposals/{proposalId}")
  public Result<String> deleteConsignProposals(@PathVariable Integer proposalId) {
    Integer consign = proposalSignersService.deleteConsignProposals(proposalId);
    if(consign == 1) {
      return ResultUtil.fail("该提案不存在，无法取消。");
    } else if (consign == 2) {
      return ResultUtil.fail("非提案人，不可取消。");
    }else if(consign == 3) {
      return ResultUtil.fail("无法取消，联名人已确认");
    }else {
      return ResultUtil.success("提案取消成功！");
    }
  }
}
